package com.jzqf.android.alive

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.jzqf.android.alive.navi.NaviActivity
import com.jzqf.android.alive.test.KeepAliveManager
import java.util.Timer
import java.util.TimerTask

class MainActivity : AppCompatActivity() {

    private val TAG = "MainActivity"
    private val PERMISSION_LOCATION_REQUEST_CODE = 1001

    private val naviBtn: Button by lazy {
        findViewById(R.id.navi_btn)
    }
    private val aliveBtn: Button by lazy {
        findViewById(R.id.alive_btn)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    override fun onStart() {
        super.onStart()
         val timer = Timer()
        aliveBtn.setOnClickListener {
//            val intent = Intent(this, AliveActivity::class.java)
//            startActivity(intent)

            Log.d(TAG, "开始执行定时任务...")
            KeepAliveManager.start(this@MainActivity){
                doTimerTask()
            }
        }
        naviBtn.setOnClickListener { startNavi() }
    }

    private fun doTimerTask() {
        val timer = Timer()
        timer.schedule(object : TimerTask() {
            override fun run() {
                Log.d(TAG, "定时任务执行中...")
            }
        }, 0, 1000L)
    }
    //开启导航
    private fun startNavi() {
        if (!this.checkLocationPermissionStatus()) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
                PERMISSION_LOCATION_REQUEST_CODE
            )
            return
        }
        val intent = Intent(this, NaviActivity::class.java)
        startActivity(intent)
    }

    private fun checkLocationPermissionStatus(): Boolean {
        if (ContextCompat.checkSelfPermission(
                baseContext, Manifest.permission.ACCESS_FINE_LOCATION
            ) == PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(
                baseContext, Manifest.permission.ACCESS_COARSE_LOCATION
            ) == PackageManager.PERMISSION_GRANTED
        ) {
            Log.i(TAG, "位置权限状态：仅使用期间允许")
            return true;
        } else {
            return false;
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int, permissions: Array<out String>, grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        if (requestCode == PERMISSION_LOCATION_REQUEST_CODE) {
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.i(TAG, "用户授予了位置权限")
            } else {
                Log.i(TAG, "用户拒绝了位置权限")
            }
        }
    }
}
